import org.example.SecRole
import org.example.SecUser
import org.example.SecUserSecRole

class BootStrap {

	def springSecurityService
	
    def init = { servletContext ->
	/***************** SECURITY ACCESS ********************/
		def userRole = SecRole.findByAuthority('ROLE_USER') ?: new SecRole(authority: 'ROLE_USER').save(failOnError: true)
		def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
		
		def adminUser = SecUser.findByUsername('admin') ?: new SecUser(
			username: 'admin',
			password: springSecurityService.encodePassword('admin'),
			authority: 'ROLE_USER',
			enabled: true).save(failOnError: true)
			
		if (!adminUser.authorities.contains(userRole)) {
            SecUserSecRole.create adminUser, userRole
        }
			
	}
	
    def destroy = {
   
    }

}
